# -*- coding: utf-8 -*-
# @日期 : 2022/1/28 0028
# @作者 : sean
# @版本 : v1.0
# @功能 :

import  requests
import  os
import time
from bs4 import  BeautifulSoup


if __name__ == '__main__':

    # 创建目录
    if not os.path.exists(f"./images/"):
        os.makedirs(f'./images/')

    url='https://www.umei.cc/bizhitupian/weimeibizhi/'
    res= requests.get(url)
    res.encoding='utf-8' #处理乱码

    try:
        # 获取bs对象
        obj = BeautifulSoup(res.text,"html.parser")
        alist = obj.find("div",class_="TypeList").find_all("a")
        for a in alist:
            href=a.get('href') # 直接通过get就可以获得属性值

            # 拿到子页面源代码
            child_page=requests.get(href)
            child_page.encoding ='utf-8'
            # 获取图片地址
            child_obj=BeautifulSoup(child_page.text,"html.parser")
            pic_obj=child_obj.find("image",attrs = {"class":"bigpic"})
            pic_src=pic_obj.get("src")
            # 下载图片
            pic_data = requests.get(pic_src).content

            with open('./images/',mode="wb") as fp:
                fp.write(pic_data)

            time.sleep(1) #每次执行后休息1s

    except Exception as result:
        print(f"未知错误{result}")


    print("over")
















